<?php

namespace app\weixin\controller;

use app\common\controller\LiteBase;
use app\common\model\OnlineWriteFlow;
use app\common\util\Assert;
use app\common\util\DateTimeUtil;
use app\common\util\LoginManager;
use app\common\util\ValueBag;
use think\Input;

class OnlineWrite extends LiteBase
{
    public function index()
    {
        if (ccnn_is_weixin_request()) {
            LoginManager::doCheckLogin();
        }
        $id = I('get.id', 0, 'intval');
        $model = [];
        if (!empty($id)) {
            $model = \app\common\model\OnlineWrite::instance()->where(['id' => $id])->find();
            if (!empty($model)) {
                if ($model['status'] != 2) {
                    $model = [];
                }
            }
        }
        $this->assign('m', ValueBag::wrap($model));
        return $this->fetch();
    }

    public function save()
    {
        if (IS_POST) {
            try {
                $db = \app\common\model\OnlineWrite::instance();
                $postData = Input::post();
                $saveData = Assert::notEmpty($postData, ['lessor_name', 'lessor_identity',
                    'lessor_postcode', 'lessor_telephone', 'lease_area', 'lease_address',
                    'lease_measure', 'lease_price', 'lease_start_date', 'lease_end_date', 'owner_name', 'owner_identity', 'lease_trustee', 'lease_trustee_phone', 'lease_trustee_identity', 'renter_name', 'renter_identity', 'renter_postcode', 'renter_telephone', 'rent_trustee', 'rent_trustee_phone',
                    'rent_trustee_identity', 'renter_address', 'lessor_address','material_name']);
                if (!isset($postData['lessor_type'])) {
                    return [false, '请选择出租人类型'];
                }

                if (!empty($postData['id']) && \app\common\model\OnlineWrite::instance()
                        ->where(['id' => $postData['id']])->getField('status') == 2
                ) {
                    $saveData['lessor_type'] = intval($postData['lessor_type']);
                    $saveData['status'] = 0;
                    $saveData['modify_time'] = DateTimeUtil::now();
                    $db->where(['id' => $postData['id']])->save($saveData);
                    OnlineWriteFlow::instance()->add([
                        'online_write_id' => $postData['id'],
                        'status' => 3,
                        'content' => '重新提交',
                        'create_time' => $saveData['modify_time']
                    ]);
                    return $this->jsonSuccess('提交成功');
                } else {
                    if (LoginManager::isLoginOn()) {
                        if ($db->where(['member_id' => LoginManager::getLoginUserId(), 'status' => ['in', [0, 2]]])->count()) {
                            return $this->jsonFail('您上次提交的预约申请还在申请中,审核通过后才能再次提交');
                        }
                    } else if ($db->where(['lessor_identity' => $saveData['lessor_identity'], 'status' => ['in', [0, 2]]])->count()) {
                        return $this->jsonFail('您上次提交的填表还在申请中,审核通过后才能再次提交');
                    }
                    $saveData['lessor_type'] = intval($postData['lessor_type']);
                    $saveData['create_time'] = DateTimeUtil::now();
                    if (LoginManager::isLoginOn()) {
                        $saveData['member_id'] = LoginManager::getLoginUserId();
                    } else {
                        $saveData['member_id'] = 0;
                    }
                    $lastId = $db->add($saveData);
                    if ($lastId) {
                        return $this->jsonSuccess('提交成功');
                    }
                }


            } catch (\Exception $ex) {
                return $this->jsonFail($ex->getMessage());
            }
        }
        return $this->jsonFail('系统忙');
    }

    public function saveResult()
    {
        return $this->fetch('saveResult');
    }

    public function workResult()
    {
        $id = Input::get('id') + 0;
        $m = OnlineWriteFlow::instance()->where(['online_write_id' => $id])->order('id desc')->find();
        if ($m['status'] == 2) { // 审核拒绝
            $this->assign('phone', ccnn_sys_setting('客服电话', ''));
            $this->assign('reason', $m['content']);
            return $this->fetch('failResult');
        } else if ($m['status'] == 0) {
            return $this->saveResult();
        }
        $this->assign('op', 1);
        $this->assign('m', ValueBag::wrap(\app\common\model\OnlineWrite::instance()->find($id)));
        return $this->saveResult();
    }


}